<?php
/**
 * Created by PhpStorm.
 * User: xuezhileikaku
 * Email:chengyingbo88@163.com
 * Date: 2024/8/23
 * Time: 下午6:41
 */

namespace App\Event;

use Psr\EventDispatcher\ListenerProviderInterface;

class ListenerProvider implements ListenerProviderInterface
{
    private $listeners = [];

    /**
     * Add a listener for a specific event type.
     *
     * @param string $eventClass The event class the listener listens to.
     * @param callable $listener The listener callback.
     */
    public function addListener(string $eventClass, callable $listener): void
    {
        $this->listeners[$eventClass][] = $listener;
    }

    /**
     * Get listeners for a specific event.
     *
     * @param object $event The event for which listeners are needed.
     * @return iterable<callable> List of listeners.
     */
    public function getListenersForEvent(object $event): iterable
    {
        $eventClass = get_class($event);

        return $this->listeners[$eventClass] ?? [];
    }
}
